import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:s_utils/s_utils.dart';

class SDialogWidget extends StatelessWidget {
  const SDialogWidget({
    Key? key,
    this.child,
    this.height,
    this.status,
    this.message,
    this.bgColor,
    this.messageWidget,
    this.title,
    this.titleWidget,
    this.secondAction,
    this.icon,
    this.iconStatus,
    this.actions,
    this.actionTexts,
    this.padding,
    this.showFirstBtn = true,
    this.showSecondBtn = true,
    this.canBack = true,
    this.scrollable = false,
  }) : super(key: key);

  final Widget? child;
  final Widget? icon;
  final String? iconStatus;
  final double? height;
  final String? status;
  final String? title;
  final Widget? titleWidget;
  final String? message;
  final Color? bgColor;
  final Widget? messageWidget;
  final GestureTapCallback? secondAction;
  final Widget? actions;
  final List<String>? actionTexts;
  final EdgeInsetsGeometry? padding;
  final bool showFirstBtn;
  final bool showSecondBtn;
  final bool canBack;
  final bool scrollable;

  @override
  Widget build(BuildContext context) {
    Widget content = child ?? SizedBox(height: 960.r);
    Widget iconW = icon ?? SizedBox(height: 100.r);
    Widget iconFont;
    ThemeData theme = Get.theme;
    List<String> tActionTexts = actionTexts ?? ["close".tr, "ok".tr];

    if (icon == null) {
      switch (iconStatus) {
        case 'warning':
          iconFont = Icon(
            const IconData(0xf071, fontFamily: 'faSolidIcon'),
            color: theme.colorScheme.onPrimary,
            size: 86.r,
          );
          break;
        case 'error':
          iconFont = Icon(
            const IconData(0xf071, fontFamily: "faSolidIcon"),
            color: theme.colorScheme.onPrimary,
            size: 86.r,
          );
          break;
        case 'info':
        case 'success':
        default:
          iconFont = Icon(
            const IconData(0xf4a6, fontFamily: 'faSolidIcon'),
            color: theme.colorScheme.onPrimary,
            size: 86.r,
          );
          break;
      }

      iconW = Container(
        width: 240.r,
        height: 240.r,
        alignment: Alignment.center,
        constraints: BoxConstraints(
          minWidth: 160.r,
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(80.r),
          color: theme.primaryColor,
          boxShadow: [
            BoxShadow(
              color: theme.primaryColor.withValues(alpha: .27),
              blurRadius: 90.r,
              offset: Offset(0, 29.r),
            )
          ],
        ),
        child: iconFont,
      );
    }

    if (child == null) {
      Widget c = Padding(
        padding: EdgeInsets.fromLTRB(0, 90.r, 0.r, scrollable ? 50.r : 50.r),
        child: Column(
          children: [
            titleWidget ??
                Text(
                  title ?? "--",
                  style: theme.textTheme.labelLarge?.copyWith(
                    fontSize: 72.r,
                    color: theme.colorScheme.surfaceDim,
                    fontWeight: FontWeight.w800,
                  ),
                ),
            SizedBox(height: 30.r),
            messageWidget ??
                SelectableText(
                  message ?? "--",
                  textAlign: TextAlign.center,
                  style: theme.textTheme.labelMedium?.copyWith(
                    fontSize: 42.r,
                    color: theme.colorScheme.tertiary,
                  ),
                ),
          ],
        ),
      );

      content = Column(
        children: [
          scrollable
              ? Expanded(
                  child: SingleChildScrollView(child: c),
                )
              : c,
          actions ??
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  if (showFirstBtn)
                    Expanded(
                      child: GestureDetector(
                        onTap: Get.back,
                        child: Container(
                          width: 300.r,
                          height: 166.r,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: showSecondBtn ? Colors.black.withValues(alpha: .4) : theme.primaryColor,
                            borderRadius: BorderRadius.circular(83.r),
                          ),
                          child: Text(
                            tActionTexts[0],
                            style: theme.textTheme.labelLarge?.copyWith(
                              fontSize: 42.r,
                              color: theme.colorScheme.onSecondary,
                              height: 1,
                            ),
                          ),
                        ),
                      ),
                    ),
                  if (showFirstBtn && showSecondBtn) SizedBox(width: 50.r),
                  if (showSecondBtn)
                    Expanded(
                      child: GestureDetector(
                        onTap: secondAction ?? Get.back,
                        child: Container(
                          height: 166.r,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: theme.primaryColor,
                            borderRadius: BorderRadius.circular(83.r),
                          ),
                          child: Text(
                            tActionTexts[1],
                            style: theme.textTheme.labelLarge?.copyWith(
                              fontSize: 42.r,
                              color: theme.colorScheme.onPrimary,
                              height: 1,
                            ),
                          ),
                        ),
                      ),
                    ),
                ],
              ),
        ],
      );
    }

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Stack(
        children: [
          Positioned.fill(
            child: GestureDetector(
              onTap: canBack ? Get.back : null,
              child: Container(
                color: theme.primaryColor.withValues(alpha: .8),
              ),
            ),
          ),
          Positioned(
            bottom: 0,
            child: Stack(
              children: [
                Container(
                  width: Sizes.screenWidth,
                  height: scrollable ? (height ?? 800.r) : null,
                  margin: EdgeInsets.only(top: 120.r),
                  padding: padding ?? EdgeInsets.fromLTRB(69.r, 78.r, 69.r, 40.r + Sizes.bottomBarHeight),
                  decoration: BoxDecoration(
                    color: bgColor ?? theme.colorScheme.surfaceBright,
                    borderRadius: BorderRadius.vertical(top: Radius.circular(100.r)),
                  ),
                  child: content,
                ),
                Positioned(
                  left: (Sizes.screenWidth - 240.r) / 2,
                  child: iconW,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
